/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.middleware.quartz.job.warning;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.enums.MessageTypeEnum;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.hywarningmessage.entity.HyWarningMessage;
import org.jeecg.modules.wlhy.hywarningmessage.service.IHyWarningMessageService;
import org.jeecg.modules.wlhy.report.IReportService;
import org.jeecg.modules.wlhy.report.ReportServiceFactory;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 各种预警信息提醒，比如司机、车辆证件到期等等
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class WarningJobService implements Job {

    public SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private IHyCompanyConfigService companyConfigService;

    @Resource
    private IHyShippingNoteService shippingNoteService;

    @Resource
    private IHyDriverService hyDriverService;

    @Resource
    private IHyVehicleService hyVehicleService;

    @Resource
    private IHyWarningMessageService warningMessageService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Long start = System.currentTimeMillis();
        log.info("定时上报任务,执行运单上报数据开始" + now.format(new Date()));
        List<HyDriver> driverList = hyDriverService.list();
        driverList.stream().forEach(d -> {
            checkDriver(d);
        });

        List<HyVehicle> vehicleList = hyVehicleService.list();
        vehicleList.stream().forEach(v -> {
            checkVehicle(v);
        });

        log.info("定时上报任务，执行运单上报数据结束,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");

        return;
    }


    private void checkVehicle(HyVehicle v) {

        //道路运输证到期时间
        Date roadTransportExpiredDate = v.getRoadTransportExpiredDate();
        if (null == roadTransportExpiredDate) {
            return;
        }

        long roadTransportExpiredDays = DateUtil.between(new Date(), roadTransportExpiredDate, DateUnit.DAY, false);

        if (roadTransportExpiredDays < 90) {
            if (roadTransportExpiredDays > 0) {
                //即将过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(v.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_YSZDQ_WARNING.getCode())
                                .messageStatus(1)
                                .messageContent("车辆: " + v.getVehicleNumber() + " 道路运输证即将过期，有效期至 " + DateUtil.format(roadTransportExpiredDate, "yyyy-MM-dd"))
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("车辆: " + v.getVehicleNumber() + " 道路运输证即将过期，有效期至 " + DateUtil.format(roadTransportExpiredDate, "yyyy-MM-dd") + "  -> " + roadTransportExpiredDays);
            } else {
                //身份证已过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(v.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_YSZDQ_ERROR.getCode())
                                .messageStatus(1)
                                .messageContent("车辆: " + v.getVehicleNumber() + " 道路运输证已过期，有效期至 " + DateUtil.format(roadTransportExpiredDate, "yyyy-MM-dd"))
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("车辆: " + v.getVehicleNumber() + " 道路运输证已过期，有效期至 " + DateUtil.format(roadTransportExpiredDate, "yyyy-MM-dd") + "  -> " + roadTransportExpiredDays);
            }
        }

        return;
    }



    private void checkDriver(HyDriver d) {

        //身份证到期时间
        Date idCardExpiredDate = d.getIdcardExpiredDate();
        if (null == idCardExpiredDate) {
            return;
        }

        //驾驶证到期时间
        Date drivingExpiredDate = d.getValidPeriodTo();
        if (null == drivingExpiredDate) {
            return;
        }

        //从业资格证有效期
        Date qualificationExpiredDate = d.getQualificationExpiredDate();
        if (null == qualificationExpiredDate) {
            return;
        }

        long idCardExpiredDays = DateUtil.between(new Date(), idCardExpiredDate, DateUnit.DAY, false);
        long drivingExpiredDays = DateUtil.between(new Date(), drivingExpiredDate, DateUnit.DAY, false);
        long qualificationExpiredDays = DateUtil.between(new Date(), qualificationExpiredDate, DateUnit.DAY, false);

        if (idCardExpiredDays < 90) {
            if (idCardExpiredDays > 0) {
                //身份证即将过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(d.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_SFZDQ_WARNING.getCode())
                                .messageStatus(1)
                                .messageContent("司机: " + d.getDriverName() + " 身份证即将过期，有效期至 " + DateUtil.format(idCardExpiredDate, "yyyy-MM-dd"))
                                .userId(d.getUserId())
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("司机: " + d.getDriverName() + " 身份证即将过期，有效期至 " + DateUtil.format(idCardExpiredDate, "yyyy-MM-dd") + "  -> " + idCardExpiredDays);
            } else {
                //身份证已过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(d.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_SFZDQ_ERROR.getCode())
                                .messageStatus(1)
                                .messageContent("司机: " + d.getDriverName() + " 身份证已过期，有效期至 " + DateUtil.format(idCardExpiredDate, "yyyy-MM-dd"))
                                .userId(d.getUserId())
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("司机: " + d.getDriverName() + " 身份证已过期，有效期至 " + DateUtil.format(idCardExpiredDate, "yyyy-MM-dd") + "  -> " + idCardExpiredDays);
            }
        }


        if (drivingExpiredDays < 90) {
            if (drivingExpiredDays > 0) {
                //即将过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(d.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_JSZDQ_WARNING.getCode())
                                .messageStatus(1)
                                .messageContent("司机: " + d.getDriverName() + " 驾驶证即将过期，有效期至 " + DateUtil.format(drivingExpiredDate, "yyyy-MM-dd"))
                                .userId(d.getUserId())
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("司机: " + d.getDriverName() + " 驾驶证即将过期，有效期至 " + DateUtil.format(drivingExpiredDate, "yyyy-MM-dd")+ "  -> " + drivingExpiredDays);
            } else {
                //已过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(d.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_JSZDQ_ERROR.getCode())
                                .messageStatus(1)
                                .messageContent("司机: " + d.getDriverName() + " 驾驶证已过期，有效期至 " + DateUtil.format(drivingExpiredDate, "yyyy-MM-dd"))
                                .userId(d.getUserId())
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("司机: " + d.getDriverName() + " 驾驶证已过期，有效期至 " + DateUtil.format(drivingExpiredDate, "yyyy-MM-dd")+ "  -> " + drivingExpiredDays);
            }
        }

        if (qualificationExpiredDays < 90) {
            if (qualificationExpiredDays > 0) {
                //即将过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(d.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_CYZGZDQ_WARNING.getCode())
                                .messageStatus(1)
                                .messageContent("司机: " + d.getDriverName() + " 从业资格证即将过期，有效期至 " + DateUtil.format(qualificationExpiredDate, "yyyy-MM-dd"))
                                .userId(d.getUserId())
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("司机: " + d.getDriverName() + " 从业资格证即将过期，有效期至 " + DateUtil.format(qualificationExpiredDate, "yyyy-MM-dd")+ "  -> " + qualificationExpiredDays);
            } else {
                //已过期
                warningMessageService.saveWarningMessage(
                        HyWarningMessage.builder()
                                .objectId(d.getId())
                                .messageType(MessageTypeEnum.MESSAGE_TYPE_CYZGZDQ_ERROR.getCode())
                                .messageStatus(1)
                                .messageContent("司机: " + d.getDriverName() + " 从业资格证已过期，有效期至 " + DateUtil.format(qualificationExpiredDate, "yyyy-MM-dd"))
                                .userId(d.getUserId())
                                .createBy("system")
                                .createTime(new Date())
                                .build()
                );
                log.info("司机: " + d.getDriverName() + " 从业资格证已过期，有效期至 " + DateUtil.format(qualificationExpiredDate, "yyyy-MM-dd")+ "  -> " + qualificationExpiredDays);
            }
        }

        return;
    }

}
